package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmNewsDto;
import com.heima.model.wemedia.dto.WmNewsPageReqDto;
import com.heima.wemedia.service.WmNewsService;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author shitou
 * @className WmNewsController
 * @description: 文章管理
 */
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    /**
     * 查询自媒体文章
     *
     * @param wmNewsPageReqDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        if (wmNewsPageReqDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmNewsService.newList(wmNewsPageReqDto);
    }

    /**
     * 文章发布
     *
     * @param wmNewsDto
     * @return
     */

    @PostMapping("/submit")
    public ResponseResult list(@RequestBody WmNewsDto wmNewsDto) throws IOException, TesseractException {

        return wmNewsService.submit(wmNewsDto);
    }

    /**
     * 文章查看详情
     *
     * @param id 文章id
     * @return
     */

    @GetMapping("/one/{id}")
    public ResponseResult getNewsById(@PathVariable("id") Integer id) {

        return wmNewsService.getNewsById(id);
    }

    /**
     * 文章删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {

        return wmNewsService.delNews(id);
    }

    /**
     * 文章上下架
     *
     * @param wmNewsDto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResponseResult delNews(@RequestBody WmNewsDto wmNewsDto) {

        return wmNewsService.downOrUp(wmNewsDto);
    }


    // todo 自动审核


}
